package cn.ilikes.modules.pasers;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import cn.ilikes.modules.likes.dbs.IlikesContent;
import cn.ilikes.modules.likes.modules.folders.IilikesContentService;
import cn.ilikes.modules.likes.modules.folders.IilikesMylikesException;
import cn.ilikes.tools.htmls.PaserException;
import cn.ilikes.tools.htmls.PaserListener;
import cn.ilikes.tools.htmls.plugins.JSoupHtmlPaser;
import cn.ilikes.tools.logs.Loggerfactory;

@Component(IHTMLPaser.SPRING_SERVICE)
public class IHTMLPaserImpls implements IHTMLPaser {
          private Logger  logger= Loggerfactory.instance(IHTMLPaserImpls.class);
	private JSoupHtmlPaser jsoupHtmlPaser = new JSoupHtmlPaser();

	@Resource(name = IilikesContentService.SPRING_SERVICE)
	private IilikesContentService likesContentService;

	@Override
	public void htmlPaser(String likesContentId) {
		try {
			final IlikesContent likesContent = likesContentService.get(likesContentId);
			String url = likesContent.getUrl();
			jsoupHtmlPaser.paser(url, new PaserListener() {
				public void result(String html, String title, String keywords, String description, String content) {
                                                  if(Loggerfactory.isdebug(logger))Loggerfactory.debug(logger, "  content:  "+content);
					likesContent.setTitle(title);
					likesContent.setHtml(html);
					likesContent.setContent(content);
;					try {
						likesContentService.update(likesContent);
					  } catch (IilikesMylikesException e) {
						e.printStackTrace();
					}
				}
			});

		} catch (IilikesMylikesException e) {
			e.printStackTrace();
		} catch (PaserException e) {
			e.printStackTrace();
		}

	}

}
